package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.util.DateFormatUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.change.KiteInstChange;
import com.iwhalecloud.bss.kite.service.attr.data.CustAttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

public class DomainNameTestAttrHandler extends AbstractAttrHandler<KiteInst, CustAttrData> {
    @Autowired
    private IQuerySceneInstService querySceneInstService;
    @Override
    public void before(KiteAttr attr, KiteInst inst, String value, CustAttrData data) {
        //服务结束时间不能小于服务开始时间
        if (KiteStringUtils.isEqual("200002505", attr.getFieldName())) { //服务起始时间
            KiteAttr endTimeAttr = inst.getAttr("200002506");
            if (null!=endTimeAttr&&KiteStringUtils.isNotEmpty(value) && KiteStringUtils.isNotEmpty(endTimeAttr.getValue())) {
                Date startDate = DateFormatUtils.parseStrToDate(value);
                Date endDate = DateFormatUtils.parseStrToDate(endTimeAttr.getValue());
                if (startDate.after(endDate)) {
                    ErrorConsts.BUSI_COMMON_ERROR.throwOut("服务起始时间必须小于服务截止时间");
                }
            }
        }

        if (KiteStringUtils.isEqual("200002506", attr.getFieldName())) { //服务截止时间
            KiteAttr startTimeAttr = inst.getAttr("200002505");
            if (null!=startTimeAttr&&KiteStringUtils.isNotEmpty(value) && KiteStringUtils.isNotEmpty(startTimeAttr.getValue())) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date startDate = DateFormatUtils.parseStrToDate(startTimeAttr.getValue());
                Date endDate = DateFormatUtils.parseStrToDate(value);
                if (startDate.after(endDate)) {
                    ErrorConsts.BUSI_COMMON_ERROR.throwOut("服务截止时间必须大于服务起始时间");
                }
            }
        }

    }

    @Override
    public void after(KiteAttr attr, KiteInst inst, CustAttrData data) {
        //变更场景 新增附加产品
        if(KiteStringUtils.equals(data.getServiceOfferId(), ServiceOfferConsts.SERVICE_OFFER_MODIFY) && KiteStringUtils.equals(inst.getActionType().name(),KeyConsts.ACTION_TYPE_A)){
            if(KiteStringUtils.equalsAny(inst.getSpecId(),"60000288","60000287","60040167","60040210")){
                List<KiteInstChange> instChangeList = querySceneInstService.listInstChanges(data.getSceneInstId(), InstType.PROD_INST);
                for(KiteInstChange kiteInstChange : instChangeList){
                    if(KiteStringUtils.equals(kiteInstChange.getActionType().name(),KeyConsts.ACTION_TYPE_D)){
                        KiteProdInst prodInst = querySceneInstService.getProdInst(data.getCustId(), kiteInstChange.getInstId());
                        if(Objects.nonNull(prodInst) && KiteStringUtils.equalsAny(prodInst.getSpecId(),"60000288","60000287","60040167","60040210")){
                            KiteAttr attr_del_200002505 = prodInst.getAttr("200002505");
                            KiteAttr attr_del_200002506 = prodInst.getAttr("200002506");
                            KiteAttr attr_200002505 = inst.getAttr("200002505");
                            KiteAttr attr_200002506 = inst.getAttr("200002506");
                            KiteAttr attr_200003794 = inst.getAttr("200003794");//计收开始时间
                            if(!KiteObjectUtils.anyNullOrEmpty(attr_del_200002505,attr_del_200002506,attr_200002505,attr_200002506,attr_200003794)){
                                attr_200002505.setValue(attr_del_200002505.getValue());
                                attr_200002505.setValueDesc(attr_del_200002505.getValueDesc());
                                attr_200002505.setIsEdit(KeyConsts.IFTRUE_F);
                                attr_200003794.setValue(attr_del_200002505.getValue());
                                attr_200003794.setValueDesc(attr_del_200002505.getValueDesc());
                                attr_200003794.setIsEdit(KeyConsts.IFTRUE_F);
                                attr_200002506.setValue(attr_del_200002506.getValue());
                                attr_200002506.setValueDesc(attr_del_200002506.getValueDesc());
                                attr_200002506.setIsEdit(KeyConsts.IFTRUE_F);
                                break;
                            }
                        }
                    }
                }
            }else{
                KiteAttr attr_200002505 = inst.getAttr("200002505");
                KiteAttr attr_200003794 = inst.getAttr("200003794");//计收开始时间
                String nextMon = DateFormatUtils.getFirstDayInNextMonth(DateFormatUtils.getFormatedDateTime(), DateFormatUtils.DATE_FORMAT);
                attr_200002505.setValue(nextMon);
                attr_200002505.setValueDesc(nextMon);
                attr_200002505.setIsEdit(KeyConsts.IFTRUE_F);
                attr_200003794.setValue(nextMon);
                attr_200003794.setValueDesc(nextMon);
                attr_200003794.setIsEdit(KeyConsts.IFTRUE_F);
            }
        }
        if(KiteStringUtils.equalsAny(inst.getActionType().name(),KeyConsts.ACTION_TYPE_M,KeyConsts.ACTION_TYPE_K)){
            //服务开始时间
            Optional<KiteAttr> serviceBegin = Optional.of(inst.getAttr("200002505"));
            if(serviceBegin.isPresent()){
                serviceBegin.get().setIsEdit(KeyConsts.IFTRUE_F);
            }
            //服务结束时间
            Optional<KiteAttr> serviceEnd = Optional.of(inst.getAttr("200002506"));
            if(serviceBegin.isPresent()) {
                serviceEnd.get().setIsEdit(KeyConsts.IFTRUE_F);
            }
            //计收方式
            Optional<KiteAttr> service_210004133 = Optional.of(inst.getAttr("210004133"));
            if(service_210004133.isPresent()) {
                service_210004133.get().setIsEdit(KeyConsts.IFTRUE_F);
            }
            //付费周期
            Optional<KiteAttr> service_200003796 = Optional.of(inst.getAttr("200003796"));
            if(service_200003796.isPresent()) {
                service_200003796.get().setIsEdit(KeyConsts.IFTRUE_F);
            }
        }
    }
}
